package com.ruoyi.web.controller.spoken;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SpokenCourse;
import com.ruoyi.system.service.ISpokenCourseService;
import com.ruoyi.system.service.ISpokenTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "课程表管理")
@RequestMapping("/spoken/course")
@RestController
public class CourseController extends BaseController {

    @Autowired
    ISpokenCourseService courseService;

    @Autowired
    ISpokenTeacherService teacherService;


    @GetMapping("/list")
    @ApiOperation("列表查询")
    public TableDataInfo list(SpokenCourse course){
        startPage();
        return getDataTable(courseService.selectList(course));
    }

    @ApiOperation("添加课程表")
    @PostMapping
    public AjaxResult add(@RequestBody SpokenCourse course){
        return success(courseService.insert(course));
    }


    @ApiOperation("查询课程表")
    @GetMapping("/{courseId}")
    public AjaxResult getById(@PathVariable Long courseId){
        return success(courseService.selectById(courseId));
    }

    @ApiOperation("修改课程表")
    @PutMapping
    public AjaxResult edit(@RequestBody SpokenCourse course){
        boolean isExist = teacherService.selectCountByCourseId(course.getCourseId());
        if(isExist){
            return error("当前课程表已被教师关联，无法修改");
        }
        return success(courseService.update(course));
    }

    @ApiOperation("删除课程表")
    @DeleteMapping("/{courseId}")
    public AjaxResult remove(@PathVariable Long courseId){
        boolean isExist = teacherService.selectCountByCourseId(courseId);
        if(isExist){
            return error("当前课程表已被教师关联，无法删除");
        }
        return success(courseService.delete(courseId));
    }

    @ApiOperation("校验是否可以修改或删除")
    @GetMapping("/check/{courseId}")
    public AjaxResult checkIsExist(@PathVariable Long courseId){
        boolean isExist = teacherService.selectCountByCourseId(courseId);
        if(isExist){
            return error("当前课程表已被教师关联，无法操作");
        }
        return success(isExist);
    }
}
